package com.jinghong.epidemiccontrolproject.controller;
import com.jinghong.epidemiccontrolproject.pojo.User;
import com.jinghong.epidemiccontrolproject.pojo.PageBean;
import com.jinghong.epidemiccontrolproject.pojo.Result;
import com.jinghong.epidemiccontrolproject.service.UserService;
import com.jinghong.epidemiccontrolproject.utils.JwtUtils;
import com.jinghong.epidemiccontrolproject.utils.Md5Util;
import com.jinghong.epidemiccontrolproject.utils.ThreadLocalUtil;
import jakarta.validation.constraints.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName:UserController
 * Package:com.jinghong.epidemiccontrolproject.controller.UserController
 * Description:
 *
 * @Author: 寻月君子
 * @CreatTime:2023/12/18 20:19
 * Version:1.0
 */
@Validated
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result register(@Pattern(regexp ="\\S{5,16}$") String username,@Pattern(regexp ="\\S{5,16}$") String password) {

        //查询用户
        User u = userService.findByUserName(username);
        if (u == null) {
            //没有占用,注册
            userService.register(username, password);
            return Result.success();
        } else {
            //占用
            return Result.error("用户名已被占用");
        }
    }

    @PostMapping("/login")
    public Result login(String username,  String password) {
        //根据用户名查询用户
        User loginUser = userService.findByUserName(username);
        //判断该用户是否存在
        if (loginUser == null) {
            return Result.error("用户名错误");
        }

        //判断密码是否正确
        if (Md5Util.getMD5String(password).equals(loginUser.getPassword())) {
            //登录成功
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", loginUser.getId());
            claims.put("username",loginUser.getUsername());

            String token = JwtUtils.generateJwt(claims);
            return Result.success(token);
        }

        //登录失败, 返回错误信息
        return Result.error("密码错误");
    }

    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String username){
        log.info("分页查询, 参数: {},{},{}",page,pageSize,username);
        //调用service分页查询
        PageBean pageBean = userService.page(page,pageSize,username);
        return Result.success(pageBean);
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("批量删除操作, ids:{}",ids);
        userService.delete(ids);
        return Result.success();
    }



    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据ID查询用户信息, id: {}",id);
        User user = userService.getById(id);
        return Result.success(user);
    }

    @PutMapping ("/update")
    public Result update(@RequestBody User user){
        log.info("更新用户信息 : {}", user);
        userService.update(user);
        return Result.success();
    }

    @GetMapping("/userinfo")
    public Result userinfo(){
        log.info("通过令牌查询用户信息 : {}");
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User user = userService.findByUserName(username);
        return Result.success(user);
    }
}
